/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.gateway.server.domain.property;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;

/**
 * gateway配置相关属性.
 *
 * @author zengdegui
 * @since 2024/4/6
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@RefreshScope
@ConfigurationProperties(prefix = "gateway")
public class GatewayProperty {
    /**
     * 监控过滤器配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private MetricConfig metric = new MetricConfig();

    /**
     * 基础过滤器配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private BaseConfig base = new BaseConfig();

    /**
     * API签名配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private ApiSignConfig apiSign = new ApiSignConfig();

    /**
     * IP黑名单配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private IpBlackListConfig ipBlackList = new IpBlackListConfig();

    /**
     * API白名单配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private ApiWhiteListConfig apiWhiteList = new ApiWhiteListConfig();

    /**
     * 认证配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private AuthConfig auth = new AuthConfig();

    /**
     * 日志配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private LogConfig log = new LogConfig();

    /**
     * 监控相关属性.
     *
     * @author zengdegui
     * @since 2024/6/1
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MetricConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;
    }

    /**
     * 基础相关属性.
     *
     * @author zengdegui
     * @since 2024/6/1
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class BaseConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;
    }

    /**
     * API签名相关属性.
     *
     * @author zengdegui
     * @since 2024/6/1
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiSignConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;

        /**
         * 缓存时间，单位：分钟.
         */
        @Builder.Default
        private Integer cacheTime = 10;

        /**
         * 是否启用相对路径（签名需要用到加签）.
         */
        @Builder.Default
        private Boolean enabledRelativePath = Boolean.FALSE;

        /**
         * 忽略的API.
         */
        private List<String> ignoredApi;
    }

    /**
     * IP黑名单相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class IpBlackListConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;
    }

    /**
     * API白名单相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiWhiteListConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;

        /**
         * 忽略的API.
         */
        private List<String> ignoredApi;
    }

    /**
     * 认证相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class AuthConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;

        /**
         * 是否启用远程token校验.
         */
        @Builder.Default
        private Boolean enabledRemoteToken = Boolean.FALSE;

        /**
         * 是否启用API权限验证.
         */
        @Builder.Default
        private Boolean enabledApiPermission = Boolean.TRUE;

        /**
         * 是否启用权限缓存.
         */
        @Builder.Default
        private Boolean enabledPermissionCache = Boolean.TRUE;

        /**
         * 是否启用清理缓存.
         */
        @Builder.Default
        private Boolean enabledClearCache = Boolean.FALSE;

        /**
         * 权限缓存时间，单位：分钟.
         */
        @Builder.Default
        private Integer permissionCacheTime = 10;

        /**
         * 忽略API鉴权的API.
         */
        private List<String> ignoredApi;

        /**
         * 通用都有权限的API.
         */
        private List<String> generalApi;

        /**
         * 忽略日志记录API.
         */
        @Builder.Default
        private List<String> ignoredLogApi = List.of("/ws");
    }

    /**
     * 日志相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LogConfig {

        /**
         * 是否启用记录日志（用户登陆后）.
         */
        @Builder.Default
        private Boolean enabled = Boolean.FALSE;

        /**
         * 是否启用记录错误日志.
         */
        @Builder.Default
        private Boolean enabledError = Boolean.FALSE;

        /**
         * 忽略记录日志的API.
         */
        private List<String> ignoredApi;

        /**
         * 采样率（10：代表10%的采样率）.
         */
        @Builder.Default
        private Integer sampleRate = 100;

        /**
         * 是否启用获取请求头.
         */
        @Builder.Default
        private Boolean enabledRequestHeader = Boolean.TRUE;

        /**
         * 是否启用获取请求参数.
         */
        @Builder.Default
        private Boolean enabledRequestParam = Boolean.TRUE;

        /**
         * 是否启用获取请求体.
         */
        @Builder.Default
        private Boolean enabledRequestBody = Boolean.TRUE;

        /**
         * 请求体限制大小，超过部分丢弃，单位：KB.
         */
        @Builder.Default
        private Integer limitRequestBody = 16;

        /**
         * 是否启用获取响应头.
         */
        @Builder.Default
        private Boolean enabledResponseHeader = Boolean.FALSE;

        /**
         * 是否启用获取响应体.
         */
        @Builder.Default
        private Boolean enabledResponseBody = Boolean.FALSE;

        /**
         * 响应体限制大小，超过部分丢弃，单位：KB.
         */
        @Builder.Default
        private Integer limitResponseBody = 16;
    }
}
